package com.itcanteen.medical.patient.service.impl;

import com.itcanteen.medical.common.constant.Constant;
import com.itcanteen.medical.common.exception.AdException;
import com.itcanteen.medical.patient.entity.Patient;
import com.itcanteen.medical.patient.entity.PatientDocument;
import com.itcanteen.medical.patient.mapper.PatientDocumentMapper;
import com.itcanteen.medical.patient.mapper.PatientMapper;
import com.itcanteen.medical.patient.service.PatientDocumentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 (PatientDocument)表服务实现类

 @author makejava
 @since 2020-12-10 18:42:47 */
@Service
public class PatientDocumentServiceImpl implements PatientDocumentService {
    @Resource
    private PatientDocumentMapper patientDocumentMapper;

    @Resource
    private PatientMapper patientMapper;

    /**
     * 根据用户id查询档案
     * @param patientId 患者id
     * @return com.itcanteen.medical.patient.entity.PatientDocument
     * @throws AdException 统一异常
     */
    @Override
    public PatientDocument findByPatientId(Integer patientId) throws AdException {
        PatientDocument patientDocument = patientDocumentMapper.selectByPatientId(patientId);
        if(patientDocument==null){
            throw new AdException(Constant.ErrorMsg.RECORD_NOT_FOUND);
        }
        return patientDocument;
    }

    /**
     * 更新档案
     * @param patientDocument 档案信息
     * @return com.itcanteen.medical.patient.entity.PatientDocument
     * @throws AdException 统一异常
     */
    @Override
    public PatientDocument update(PatientDocument patientDocument) throws AdException {
        Patient patient = patientMapper.selectByPrimaryKey(patientDocument.getPatientId());
        if(patient.getStatus()==0){
            throw new AdException(Constant.PatientConstant.ABNORMAL_ERROR);
        }
        patientDocumentMapper.updateByPrimaryKey(patientDocument);
        return findByPatientId(patient.getId());
    }
}